Merge conflicts solutioned
[RRRRHHHH_Code] / ruralHouses client / src / gui / listOfOwnerAddittionRequests.java
1 package gui;
2
3 import java.awt.Font;
4 import java.awt.Rectangle;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.rmi.Naming;
8 import java.rmi.RemoteException;
9 import java.util.Enumeration;
10 import java.util.Vector;
11
12 import javax.swing.JButton;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JPanel;
16 import javax.swing.JScrollPane;
17 import javax.swing.JTable;
18 import javax.swing.border.EmptyBorder;
19 import javax.swing.table.DefaultTableModel;
20
21 import common.AccountInterface;
22 import common.AdminInterface;
23
24 import configuration.___IntNames;
25 import domain.Account;
26
27 public class listOfOwnerAddittionRequests extends JFrame {
28
29         /**
30          * 
31          */
32         private static final long serialVersionUID = 1L;
33         private JPanel contentPane;
34         private JTable table;
35         private DefaultTableModel tableModel;
36         private AdminInterface am = null;
37         private Vector<Account> accounts;
38         /**
39          * Create the frame.
40          */
41         public  listOfOwnerAddittionRequests()  {
42                 try {
43                         am = (AdminInterface) Naming
44                                         .lookup(___IntNames.AdminManager);
45                 } catch (Exception e1) {
46                         System.out.println("Error accessing remote authentication: "
47                                         + e1.toString());
48                 }               
49                 setTitle("Adding requests");
50                 try {
51                         this.accounts= am.getOwnerAdditionRequests();
52                         init();
53                 } catch (Exception e) {
54                         e.printStackTrace();
55                 }
56         }
57
58         private void init() throws Exception {
59                 
60                 try {
61                         am = (AdminInterface) Naming
62                                         .lookup(___IntNames.AdminManager);
63                 } catch (Exception e1) {
64                         System.out.println("Error accessing remote authentication: "
65                                         + e1.toString());
66                 }
67                 setBounds(100, 100, 600, 450);
68                 contentPane = new JPanel();
69                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
70                 setContentPane(contentPane);
71                 contentPane.setLayout(null);
72
73                 JLabel lblNewLabel = new JLabel();
74                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
75                 lblNewLabel.setBounds(23, 41, 536, 33);
76                 contentPane.add(lblNewLabel);
77                 if (accounts.isEmpty())
78                         lblNewLabel.setText("There are not owners to be added");
79                 else
80                         lblNewLabel.setText("List of owners to be added:");
81                 JScrollPane scrollPane = new JScrollPane();
82                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
83                 scrollPane.setBounds(23, 113, 536, 271);
84                 contentPane.add(scrollPane);
85
86                 table = new JTable() {
87                 private static final long serialVersionUID = 1L;
88
89                 public boolean isCellEditable(int row, int column) {                
90                         return false;               
91                 };
92             };
93                 scrollPane.setViewportView(table);
94                 tableModel = new DefaultTableModel(null, new String[] {
95                                 "Name", "E-mail", "Bank Account" });
96                 
97                 //Maybe there is a better way to avoid interaction.
98                 //table.setEnabled(false);
99                 table.setModel(tableModel);
100                 
101                 JButton btnNewButton = new JButton("Confirm Addition");
102                 btnNewButton.addActionListener(new ActionListener() {
103                         public void actionPerformed(ActionEvent e) {
104                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
105                                         AccountInterface acm = null;
106                                         
107                                         try {
108                                                 acm = (AccountInterface) Naming
109                                                                 .lookup(___IntNames.AdminManager);
110                                         } catch (Exception e1) {
111                                                 System.out.println("Error accessing remote authentication: "
112                                                                 + e1.toString());
113                                         }
114                                         Account accou = accounts.get(table.getSelectedRow());
115                                         try {
116                                                 acm.addAccount(accou);
117                                                 am.removeOwnerAdditionRequests(accou);
118                                                 am.saveInstance();
119                                         } catch (RemoteException e1) {
120                                                 // TODO Auto-generated catch block
121                                                 e1.printStackTrace();
122                                         }
123                         
124                                         ((DefaultTableModel)table.getModel()).removeRow(accounts.indexOf(accou));
125                                         accounts.remove(accou);
126                                 }
127                         }
128                 });
129                 btnNewButton.setBounds(88, 396, 169, 25);
130                 contentPane.add(btnNewButton);
131                 
132                 JButton btnDenyAddition = new JButton("Deny Addition");
133                 btnDenyAddition.addActionListener(new ActionListener() {
134                         public void actionPerformed(ActionEvent arg0) {
135                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
136                                         Account acc = accounts.get(table.getSelectedRow());
137                                         try {
138                                                 am.removeOwnerAdditionRequests(acc);
139                                                 am.saveInstance();
140                                         } catch (RemoteException e) {
141                                                 // TODO Auto-generated catch block
142                                                 e.printStackTrace();
143                                         }
144                                         
145                                         ((DefaultTableModel)table.getModel()).removeRow(accounts.indexOf(acc));
146                                         accounts.remove(acc);
147                                 }
148                         }
149                 });
150                 btnDenyAddition.setBounds(300, 396, 169, 25);
151                 contentPane.add(btnDenyAddition);
152                 Enumeration<Account> en = accounts.elements();
153                 Account acc;
154                 
155                 while (en.hasMoreElements()) {
156                         acc = en.nextElement();
157                         Vector<Object> row = new Vector<Object>();
158                         row.add(acc.getOwner().getName());
159                         row.add(acc.getOwner().getMailAccount());
160                         row.add(acc.getOwner().getBankAccount());                       
161                         tableModel.addRow(row);
162                 }
163
164         }
165 }